<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Clinic as ClinicModel;

/**
 * 诊所接口
 */
class Clinic extends Api
{
    protected $noNeedLogin = ['getClinic'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        
        if (isset($_SERVER['HTTP_ORIGIN'])) {
            header('Access-Control-Expose-Headers: __token__');
        }
        //跨域检测
        check_cors_request();
    }

    /**
     * 获取诊所信息
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=false, description="诊所ID，不传则获取所有诊所信息")
     */
    public function getClinic()
    {
        $id = $this->request->get('id/d', 0);
        
        if ($id > 0) {
            // 获取指定ID的诊所信息
            $clinic = ClinicModel::where('id', $id)->find();
            if (!$clinic) {
                $this->error('诊所不存在');
            }
            $this->success('获取成功', $clinic);
        } else {
            // 获取所有诊所信息
            $clinics = ClinicModel::select();
            // 确保数据是数组格式
            if (is_object($clinics)) {
                $clinics = $clinics->toArray();
            }
            $this->success('获取成功', $clinics);
        }
    }
}
